home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WINPROGS.ARJ / ENVIRON.C < prev    next >
Text File  |  1990-12-02  |  5KB  |  144 lines

  1. /*   Environ.c   -   Environment List Box
  2.                      Petzold
  3. */
  4.  
  5. #include <windows.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #define MAXENV  4096
  10.  
  11.  
  12. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  13.  
  14. int PASCAL WinMain (HANDLE hInstance,
  15.                     HANDLE hPrevInstance,
  16.                     LPSTR  lpszCmdParam,
  17.                     int    nCmdShow)
  18.                     
  19.   {
  20.   static char szAppName[] = "Environ";
  21.   HWND        hwnd;
  22.   MSG         msg;
  23.   WNDCLASS    wndclass;
  24.   
  25.   if (!hPrevInstance)
  26.      {
  27.      wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  28.      wndclass.lpfnWndProc      = WndProc;
  29.      wndclass.cbClsExtra       = 0;
  30.      wndclass.cbWndExtra       = 0;
  31.      wndclass.hInstance        = hInstance;
  32.      wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION);
  33.      wndclass.hCursor          = LoadCursor (NULL, IDC_ARROW);
  34.      wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  35.      wndclass.lpszMenuName     = NULL;
  36.      wndclass.lpszClassName    = szAppName;
  37.      
  38.      RegisterClass(&wndclass);
  39.      }
  40.      
  41.   hwnd = CreateWindow (szAppName,
  42.                        "Environment List Box",
  43.                        WS_OVERLAPPEDWINDOW, 
  44.                        CW_USEDEFAULT,   
  45.                        CW_USEDEFAULT,
  46.                        CW_USEDEFAULT,   
  47.                        CW_USEDEFAULT,   
  48.                        NULL,
  49.                        NULL,
  50.                        hInstance,
  51.                        NULL);
  52.                        
  53.   ShowWindow (hwnd, nCmdShow);
  54.   UpdateWindow (hwnd);
  55.   
  56.   while (GetMessage (&msg, NULL, 0, 0))
  57.     {
  58.     TranslateMessage (&msg);
  59.     DispatchMessage  (&msg);
  60.     }
  61.     
  62.   return msg.wParam;
  63.   }
  64.   
  65. long FAR PASCAL WndProc (HWND hwnd,
  66.                          WORD message,
  67.                          WORD wParam,
  68.                          LONG lParam)
  69.                          
  70.   {
  71.   static char szBuffer [MAXENV + 1];
  72.   static HWND hwndList, hwndText;
  73.   HDC          hdc;
  74.   TEXTMETRIC   tm;
  75.   WORD         n;
  76.   
  77.   
  78.   switch (message)
  79.     {
  80.     case WM_CREATE:
  81.       hdc = GetDC (hwnd);
  82.       GetTextMetrics (hdc, &tm);
  83.       ReleaseDC (hwnd, hdc);
  84.       
  85.       hwndList = CreateWindow ("listbox", 
  86.                                "Environment",
  87.                                WS_CHILD | WS_VISIBLE |WS_OVERLAPPED,
  88.                                tm.tmAveCharWidth,
  89.                                tm.tmHeight * 3,
  90.                                tm.tmAveCharWidth * 16 + GetSystemMetrics (SM_CXVSCROLL),
  91.                                tm.tmHeight * 5,
  92.                                hwnd,
  93.                                1,
  94.                                GetWindowWord (hwnd, GWW_HINSTANCE),
  95.                                NULL);
  96.                                 
  97.       hwndText = CreateWindow ("static",
  98.                                NULL,
  99.                                WS_CHILD | WS_VISIBLE | SS_LEFT,
  100.                                tm.tmAveCharWidth,
  101.                                tm.tmHeight,
  102.                                tm.tmAveCharWidth * MAXENV,
  103.                                tm.tmHeight,
  104.                                hwnd,
  105.                                2,
  106.                                GetWindowWord (hwnd, GWW_HINSTANCE),
  107.                                NULL);
  108.                                
  109.       for (n=0; environ[n]; n++)
  110.         {
  111.         if (strlen (environ[n]) > MAXENV)
  112.           continue;
  113.           *strchr (strcpy (szBuffer, environ[n]), '=') = '\0';
  114.           SendMessage (hwndList, LB_ADDSTRING, 0,
  115.                        (LONG) (LPSTR) szBuffer);
  116.          }
  117.          
  118.        return 0;
  119.        
  120.      case WM_SETFOCUS:
  121.        SetFocus (hwndList);
  122.        return 0;
  123.        
  124.      case WM_COMMAND:
  125.        if (wParam == 1 && HIWORD (lParam) == LBN_SELCHANGE)
  126.          {
  127.          n = (WORD) SendMessage (hwndList, LB_GETCURSEL, 0, 0L);
  128.          n = (WORD) SendMessage (hwndList, LB_GETTEXT, n,
  129.                                  (LONG) (LPSTR) szBuffer);
  130.          strcpy (szBuffer + n + 1, getenv (szBuffer));
  131.          *(szBuffer + n) = '=';
  132.          SetWindowText (hwndText, szBuffer);
  133.          }
  134.        return 0;
  135.                                                                                                
  136.     case WM_DESTROY:
  137.       PostQuitMessage (0);
  138.       return 0;
  139.     }
  140.     
  141.   return DefWindowProc (hwnd, message, wParam, lParam);
  142.   }
  143.                   
  144.